package com.example.springmybatis.controller;

import com.example.springmybatis.entity.User;
import com.example.springmybatis.service.UserService;
import com.example.springmybatis.vo.UserGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{uid}")
    public User findUserById(@PathVariable("uid") Integer uid) {

        System.out.println("findUserById userId=>"+uid);
        User user = userService.findUserById(uid);
        System.out.println("findUserById user=>"+user.toString());
        return user;
    }

    @PostMapping("/add")
    public User addUser(@RequestBody User user) {

        System.out.println("insert user=>"+user.toString());

        return userService.insertUser(user);

    }

    @PostMapping("/update")
    public User updateUser(@RequestBody User user) {

        System.out.println("update user=>"+user.toString());

        return userService.updateUser(user);
    }

    @GetMapping("/delete/{uid}")
    public void deleteUser(@PathVariable("uid") Integer uid) {

        System.out.println("delete user=>"+uid);

        userService.deleteUser(uid);

    }

    @GetMapping("/")
    public List<User> findAllUser() {

        List<User> users = userService.findAllUser();
        System.out.println("findAllUser user=>"+users);
        return users;
    }

    @GetMapping("/find/{uid}")
    public UserGroupVo findUserGroupVo(@PathVariable("uid") Integer uid) {
        UserGroupVo userGroupVo = userService.findUserGroupVo(uid);
        System.out.println("findUserGroupVo  =>" + userGroupVo.toString());
        return userGroupVo;
    }

    @GetMapping("/find")
    public List<User> findUserByCondition(User user) {
        return userService.findUserByCondition(user);
    }


}
